package com.project.springboot.controller;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
 * Description：测试Controller
 *              @SpringBootTest 不能和 @WebMvcTest 同时使用
 *              如果想使用MockMvc对象的话，需要另外加上@AutoConfigureMockMvc注解
 * Created by 朱正磊 on 2019/3/11 0011 下午 3:26
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class BookControllerTest3 {

    @Autowired
    private MockMvc mvc;

    @Test
    public void home() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/book/home"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        mvc.perform(MockMvcRequestBuilders.get("/book/home"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("bookhome"));
    }

    @Test
    public void show() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/book/show").param("id", "10"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("book10"));
    }

}